/**
 * Created by kaider on 2016/12/5.
 */
function getUri1(uri, cur, place, device,deviceType){
    var viewUri = getView(cur);
    var uri = uri+viewUri;
    if(place!=0 && device==0){
        uri = uri+"/"+place;
    }else if(place!=0 && device!=0){
        uri = uri+"/"+place+"/"+device+"/"+deviceType;
    }
    if(cur==6) {
        uri = uri + "/getAccount";
    }
    window.location.href = uri;
}

function getUri2(uri, cur){
    var viewUri = getView(cur);
    var uri = uri+viewUri;
    window.location.href = uri;
}

$(".placeAndEquipment_search").click(function () {
    var stime = $("#stime").val();
    var etime = $("#etime").val();
    $.ajax({
        url: uri+"search",
        type: 'POST',
        data: {"stime":stime,"etime":etime,"placeId":place, "deviceId":device},
        error: function (response) {
            $errorMsg(response);
        },
        success: function (ret) {
            bindingParam(ret.AccountSummary);
            if(device==""){
                getBalanceOption();
            }
        }
    });
});

$(".index_search").click(function () {
    var stime = $("#stime").val();
    var etime = $("#etime").val();
    var shortCode = $("#shortCode").val();
    var imei = $("#imei").val();
    $.ajax({
        url: uri+"search",
        type: 'POST',
        data: {"stime":stime,"etime":etime,"shortCode":shortCode, "imei":imei},
        error: function (response) {
            $errorMsg(response);
        },
        success: function (ret) {
            bindingParam(ret.AccountSummary);
            getBalanceOption();
            getLineChart(ret.dayVal,ret.dataVal);
            getStackBarChart(ret.dayVal,ret.weChatVal,ret.aliPayVal.replace(/,/g,''),ret.sumPayVal.replace(/,/g,''));
        }
    });
});

//时间条件查询加载报表
function getBalanceOption(){
    var cashIncome = $("#GC_balance_cashIncome").text().replace(/,/g,'');
    var iconIncome = $("#GC_balance_iconIncome").text().replace(/,/g,'');
    var wechatpayIncome = $("#GC_balance_wechatpayIncome").text().replace(/,/g,'');
    var alipayIncome = $("#GC_balance_alipayIncome").text().replace(/,/g,'');
    var gameCoinQuantity = $("#GC_balance_gameCoinQuantity").text().replace(/,/g,'');
    var outGameCoinQuantity = $("#GC_balance_outGameCoinQuantity").text().replace(/,/g,'');
    var giftSellQuantity = $("#GC_balance_giftSellQuantity").text().replace(/,/g,'');
    var remotelyOutGameCoinQuantity = $("#GC_balance_remotelyOutGameCoinQuantity").text().replace(/,/g,'');

    //条形统计图
    var myBarChart=echarts.init(document.getElementById("indexpage_barchart"));
    var barOption = getBarOption(cashIncome,iconIncome,wechatpayIncome,
        alipayIncome,gameCoinQuantity,outGameCoinQuantity,
        giftSellQuantity,remotelyOutGameCoinQuantity
    );
    myBarChart.setOption(barOption);

    //百分比统计图
    var myPieChart=echarts.init(document.getElementById("indexpage_ratechart"));
    var pieOption = getPieOption(cashIncome,iconIncome,wechatpayIncome,alipayIncome);
    myPieChart.setOption(pieOption);

}
//折线图
function getLineChart(dayVal,dataVal){
    var dayVal = dayVal;
    var dataVal = dataVal;
    var myLineChart=echarts.init(document.getElementById("indexpage_linechart"));
    var lineOption = getLineOption(dayVal,dataVal);
    myLineChart.setOption(lineOption);
}

//堆叠条形图
function getStackBarChart(dayVal,weChatVal,aliPayVal,sumPayVal){
    var dayVal = dayVal;
    var weChat = weChatVal;
    var aliPay = aliPayVal;
    var sumPay = sumPayVal;
    var myStackBarChart=echarts.init(document.getElementById("indexpage_stackbarchart"));
    var stackOption = getStackBarOption(dayVal,weChat,aliPay,sumPay);
    myStackBarChart.setOption(stackOption);
}

//绑定参数
function bindingParam(ret) {
    //保留的小数位
    var n = 2;
    $("#GC_balance_time").text(ret.createDate);

    $("#deviceInfo").text(ret.deviceInfo);

    $("#GC_balance_grossIncome").text(ret.grossIncome.replace(/,/g,''));
    $("#GC_balance_payIncome").text(eval(ret.wechatpayIncome+"+"+ ret.alipayIncome).replace(/,/g,''));
    $("#GC_balance_moneyIncome").text(ret.moneyIncome.replace(/,/g,''));
    $("#GC_balance_cashIncome").text(ret.cashIncome.replace(/,/g,''));

    $("#GC_balance_oneBalance").text(ret.oneBalance.replace(/,/g,''));
    $("#GC_balance_oneBalance_money").text(fmoney(ret.oneBalance * 1,n));
    $("#GC_balance_fiveBalance").text(ret.fiveBalance);
    $("#GC_balance_fiveBalance_money").text(fmoney(ret.fiveBalance * 5,n));
    $("#GC_balance_tenBalance").text(ret.tenBalance);
    $("#GC_balance_tenBalance_money").text(fmoney(ret.tenBalance * 10,n));
    $("#GC_balance_twentyBalance").text(ret.twentyBalance);
    $("#GC_balance_twentyBalance_money").text(fmoney(ret.twentyBalance * 20,n));
    $("#GC_balance_fiftyBalance").text(ret.fiftyBalance);
    $("#GC_balance_fiftyBalance_money").text(fmoney(ret.fiftyBalance * 50,n));
    $("#GC_balance_hundredBalance").text(ret.hundredBalance);
    $("#GC_balance_hundredBalance_money").text(fmoney(ret.hundredBalance * 100,n));

    $("#GC_balance_iconIncome").text(ret.iconIncome.replace(/,/g,''));
    $("#GC_balance_netpayIncome").text(ret.netpayIncome.replace(/,/g,''));
    $("#GC_balance_wechatpayIncome").text(ret.wechatpayIncome.replace(/,/g,''));
    $("#GC_balance_alipayIncome").text(ret.alipayIncome.replace(/,/g,''));
    $("#GC_balance_gameCoinQuantity").text(ret.gameCoinQuantity.replace(/,/g,''));
    $("#GC_balance_outGameCoinQuantity").text(ret.outGameCoinQuantity.replace(/,/g,''));
    $("#GC_balance_remotelyOutGameCoinQuantity").text(ret.remotelyOutGameCoinQuantity.replace(/,/g,''));
    $("#GC_balance_giftSellQuantity").text(ret.giftSellQuantity.replace(/,/g,''));
    $("#GC_balance_giftstockQuantity").text(ret.giftstockQuantity.replace(/,/g,''));
}

function getView(cur){
    var viewUri;
    switch (cur){
        case 0:
            viewUri = "todayView";
            break;
        case 1:
            viewUri = "weekView";
            break;
        case 2:
            viewUri = "monthView";
            break;
        case 3:
            viewUri = "yesterdayView";
            break;
        case 4:
            viewUri = "lastWeekView";
            break;
        case 5:
            viewUri = "lastMonthView";
            break;
        case 6:
            viewUri = "frequencyView";
            break;
        case 7:
            viewUri = "ledgerView";
            break;
        default:
            viewUri = "todayView";
    }

    return viewUri;
}

//s:传入的float数字 ，n:希望返回小数点几位
function fmoney(s, n) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(),
        r = s.split(".")[1];
    t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("") + "." + r;
}